﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Hjp.FrameWork.Common;
using Wjtour.Domain;
using Wjtour.BLL;

namespace Wjtour.Web.Admin.role
{
    public partial class addedi : BasePage
    {
        protected TextBox title;
        protected HiddenField rights;
        protected Button submit;
        protected int id = 0;
        Wjtour.Domain.webrole link = new Domain.webrole();
        protected List<Wjtour.Domain.dto.menu_menuaccess> menus = new List<Domain.dto.menu_menuaccess>();
        menu_bll _bll = new menu_bll();
        webrole_bll _rolebll = new webrole_bll();
        protected void Page_Load(object sender, EventArgs e)
        {
            id = Utils.StrToInt(Request["id"], 0);
            submit.Click += new EventHandler(submit_Click);
            renderMenuTree();
            if (!IsPostBack)
            {
                if (id > 0)
                {
                    link = _rolebll.Load(id);
                    if (link != null)
                    {
                        title.Text = link.title;
                    }
                }
            }
        }

        void renderMenuTree()
        {
            menus = _bll.GetMenusWithAccessRightFlag(id);
        }

        void submit_Click(object sender, EventArgs e)
        {
            string[] ids = rights.Value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            string t = title.Text;
            if (string.IsNullOrWhiteSpace(t))
            {
                Utils.Alert(this.Page, "名称不能为空！");
                return;
            }
            bool b = _rolebll.UpdateRights(id, t, ids);

            if (b)
            {
                SaveLog(id == 0 ? "Add" : "edit");
                Utils.Alert(this.Page, "保存成功！");
                Utils.Redirect(this.Page, "list.aspx", false);
            }
        }
    }
}